package com.gdpu.service.impl;

import com.gdpu.api.CommonPage;
import com.gdpu.mapper.PmsCategoryMapper;
import com.gdpu.mapper.PmsGoodsMapper;
import com.gdpu.model.PmsCategory;
import com.gdpu.model.PmsCategoryExample;
import com.gdpu.model.PmsGoods;
import com.gdpu.model.PmsGoodsExample;
import com.gdpu.service.PmsCategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:weiming
 * @Date:2021/5/27
 */
@Service
public class PmsCategoryServiceImpl implements PmsCategoryService {

    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;
    @Autowired
    private PmsGoodsMapper pmsGoodsMapper;

    @Override
    public CommonPage list(Integer pageNum, Integer pageSize) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        PmsCategoryExample example = new PmsCategoryExample();
        example.setOrderByClause("order desc");
        example.createCriteria().andIsDeletedEqualTo(0);
        List<PmsCategory> pmsCategories = pmsCategoryMapper.selectByExample(example);
        return CommonPage.restPage(pmsCategories, page);
    }


    @Override
    public PmsCategory get(Integer id) {
        return null;
    }

    @Override
    public int add(PmsCategory pmsCategory) {
        return 0;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public int update(PmsCategory pmsCategory) {
        return 0;
    }

    @Override
    public CommonPage listByParentId(Integer categoryId, int pageNum, int pageSize) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        PmsGoodsExample example = new PmsGoodsExample();
        example.setOrderByClause("sort desc");
        example.createCriteria().andIsDeletedEqualTo(0).andCategoryIdEqualTo(categoryId);
        List<PmsGoods> pmsGoods = pmsGoodsMapper.selectByExample(example);
        return CommonPage.restPage(pmsGoods,page);
    }

    @Override
    public List<PmsCategory> getCategorysById(Integer id) {
        PmsCategoryExample example = new PmsCategoryExample();
        example.setOrderByClause("sort desc");
        PmsCategoryExample.Criteria criteria = example.createCriteria().andIsDeletedEqualTo(0);
        if (id != null) {
            criteria.andParentIdEqualTo(id);
        }else{
            criteria.andParentIdIsNull();
        }

        List<PmsCategory> pmsCategoryList = pmsCategoryMapper.selectByExample(example);
        return pmsCategoryList;
    }
}
